package com.yesorno.activity;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.yesorno.constants.Constants;
import com.yesorno.model.HttpResponseObject;
import com.yesorno.ui.CustomTitleActivity;
import com.yesorno.ui.MyMenuList;
import com.yesorno.utils.HttpUtils;

public class TopicDetailActivity extends CustomTitleActivity {
	private Button goBack;
	private Button comments;
	private Button topic_detail_yes;
	private Button topic_detail_no;
	private Button topic_detail_title_btn_info;
	RegBtnsClickListeners rbc = new RegBtnsClickListeners();

	private ImageView user_logo;
	private ImageView topic_detail_img;
	private ImageView topic_detail_yes_commented;
	private ImageView topic_detail_no_commented;

	private TextView user_name;
	private TextView topic_detail_desc;
	private TextView topic_detail_from;
	private TextView topic_detail_time;

	int count_yes = 0;
	int count_no = 0;
	String count_yes_str = "";
	String count_no_str = "";

	int type_yes = 0;
	int type_no = 1;

	private long currentTopicId;

	/**
	 * 直接评论后的返回处理
	 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (resultCode) {
		case RESULT_OK:
			int type = data.getIntExtra("type", type_yes);
			int result = data.getIntExtra("result", 1);
			if (result != 0) {
				Toast.makeText(TopicDetailActivity.this,
						data.getStringExtra("msg"), 1000).show();
			} else {
				if (type == type_yes) {
					count_yes++;
					topic_detail_yes.setText(count_yes_str.replace("$1",
							count_yes + ""));
					topic_detail_yes_commented.setVisibility(View.VISIBLE);
				}
				if (type == type_no) {
					count_no++;
					topic_detail_no.setText(count_no_str.replace("$1", count_no
							+ ""));
					topic_detail_no_commented.setVisibility(View.VISIBLE);
				}
			}
			break;
		default:
			break;
		}
		super.onActivityResult(requestCode, resultCode, data);

	}

	private TextView topic_detail_img_path;

	/**
	 * 注册页面按钮的监听类
	 * 
	 * @author samoin
	 * @date 2011-07-06
	 * @version 0.1
	 * 
	 */
	class RegBtnsClickListeners implements OnClickListener {
		@Override
		public void onClick(View v) {
			if (v == goBack) {
				Intent intent = new Intent();
				TopicDetailActivity.this.setResult(RESULT_OK, intent);
				TopicDetailActivity.this.finish();
			}
			if (v == comments) {
				Intent intent = new Intent();
				intent.putExtra("currentTopicId", currentTopicId);
				intent.setClass(TopicDetailActivity.this,
						ViewCommentsActivity.class);
				startActivity(intent);
			}
			if (v == topic_detail_yes) {
				Intent intent = new Intent();
				intent.putExtra("type", type_yes);
				intent.putExtra("currentTopicId", currentTopicId);
				intent.setClass(TopicDetailActivity.this,
						CommentMenuActivity.class);
				// 妈妈咪啊，这个太万恶了，居然因为这个启动方式耽误了我半天功夫，关键是之前有过这个功能，我日
				startActivityForResult(intent, 0);
			}
			if (v == topic_detail_no) {
				Intent intent = new Intent();
				intent.putExtra("type", type_no);
				intent.putExtra("currentTopicId", currentTopicId);
				intent.setClass(TopicDetailActivity.this,
						CommentMenuActivity.class);
				// 这句话很重要，否则会无法接收到下个activity返回的数据,尤其是后面的这个参数---0
				startActivityForResult(intent, 0);
			}
			if (v == topic_detail_title_btn_info) {
				Intent intent = new Intent();
				intent.putExtra("currentUserId", currentTopicId);
				intent.setClass(TopicDetailActivity.this,
						UserInfoActivity.class);
				startActivity(intent);
			}
			if (v == topic_detail_img) {
				Intent intent = new Intent();
				intent.putExtra("imgPath", topic_detail_img_path.getText()
						.toString());
				intent.setClass(TopicDetailActivity.this,
						ImageDetailActivity.class);
				startActivity(intent);
			}
		}

	}

	@Override
	public void setTitleViews() {
		LinearLayout titleLayout = getTITLE_LAYOUT();
		goBack = CustomTitleActivity.createTitleLeftButton(
				TopicDetailActivity.this, R.string.reg_title_btn_goback,
				Constants.TITLE_BTN_MARGIN1);
		goBack.setOnClickListener(rbc);
		titleLayout.addView((View) goBack.getParent());
		TextView tv = CustomTitleActivity.createTitleDesc(
				TopicDetailActivity.this, R.string.topic_detail_title, 0);
		titleLayout.addView((View) tv.getParent());

		comments = CustomTitleActivity.createTitleRightButton(
				TopicDetailActivity.this,
				R.string.topic_detail_title_btn_watchcomments,
				Constants.TITLE_BTN_MARGIN2);
		comments.setOnClickListener(rbc);
		LinearLayout ll = (LinearLayout) comments.getParent();
		ll.setGravity(Gravity.RIGHT);
		titleLayout.addView((View) comments.getParent());
	}

	@Override
	public void setLayoutId() {
		setContentView(R.layout.topic_detail);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		currentTopicId = getIntent().getLongExtra("currentTopicId", 0);
		topic_detail_yes = (Button) findViewById(R.id.topic_detail_yes);
		topic_detail_no = (Button) findViewById(R.id.topic_detail_no);
		topic_detail_title_btn_info = (Button) findViewById(R.id.topic_detail_title_btn_info);
		topic_detail_yes.setOnClickListener(rbc);
		topic_detail_no.setOnClickListener(rbc);
		topic_detail_title_btn_info.setOnClickListener(rbc);

		user_logo = (ImageView) findViewById(R.id.user_logo);
		topic_detail_img = (ImageView) findViewById(R.id.topic_detail_img);
		topic_detail_img.setOnClickListener(rbc);

		user_name = (TextView) findViewById(R.id.user_name);
		topic_detail_desc = (TextView) findViewById(R.id.topic_detail_desc);
		topic_detail_from = (TextView) findViewById(R.id.topic_detail_from);
		topic_detail_time = (TextView) findViewById(R.id.topic_detail_time);
		topic_detail_img_path = (TextView) findViewById(R.id.topic_detail_img_path);

		topic_detail_yes_commented = (ImageView) findViewById(R.id.topic_detail_yes_commented);
		topic_detail_no_commented = (ImageView) findViewById(R.id.topic_detail_no_commented);

		// 加载菜单
		((LinearLayout) findViewById(R.id.menus)).addView(new MyMenuList(
				TopicDetailActivity.this, 0));
		HttpResponseObject hro = HttpUtils.getInfoByUrlPost(
				Constants.TOPIC_DETAIL_URL + "topicId=" + currentTopicId,
				TopicDetailActivity.this);
		JSONObject jsonObject;
		// String temp =
		// "{topicId:1,authorId:1,authorName:\"小明\",imgs:\"" +
		// Constants.BASE_HTTP_CONTEXT +
		// "img/mm.jpg\",title:\"美图的标题\",yesCount:10,noCount:5,from:\"android客户端\",publishTime:\"2011-09-08 12:12\"}";
		// HttpResponseObject hro = new HttpResponseObject();
		// hro.setCode(1);
		// hro.setMessages("ok");
		// JSONObject jsonObject = new JSONObject();
		// hro.setResult(jsonObject);
		// jsonObject.put("desc",
		// "图片图片看一看，好无聊的图片图片图片看一看，好无聊的图片图片图片看一看，好无聊的图片图片图片看一看，好无聊的图片图片图片看一看，好无聊的图片图片图片看一看");
		try {
			jsonObject = hro.getResult().getJSONObject("topic");
			String desc = jsonObject.getString("desc");
			String imgs = jsonObject.getString("imgs");
			count_yes = jsonObject.getInt("yesCount");
			count_no = jsonObject.getInt("noCount");
			String from = jsonObject.getString("from");
			String publishTime = jsonObject.getString("publishTime");
			String authorName = jsonObject.getString("authorName");
			String authorLogo = jsonObject.getString("authorLogo");
			Boolean isCommentYes = jsonObject.getBoolean("isCommentYes");
			Boolean isCommentNo = jsonObject.getBoolean("isCommentNo");

			if (isCommentYes) {
				topic_detail_yes_commented.setVisibility(View.VISIBLE);
			}
			if (isCommentNo) {
				topic_detail_no_commented.setVisibility(View.VISIBLE);
			}

			user_name.setText(authorName);
			topic_detail_desc.setText(desc);
			topic_detail_from.setText("来自：" + from);
			topic_detail_time.setText(publishTime);
			count_yes_str = topic_detail_yes.getText().toString();
			topic_detail_yes.setText(count_yes_str
					.replace("$1", count_yes + ""));
			count_no_str = topic_detail_no.getText().toString();
			topic_detail_no.setText(count_no_str.replace("$1", count_no + ""));
			topic_detail_yes.setVisibility(View.VISIBLE);
			topic_detail_no.setVisibility(View.VISIBLE);
			topic_detail_img_path.setText(imgs);
			// System.out.println("nowThread-topic:"
			// + Thread.currentThread().getId());
			Handler handler = new Handler();
			CustomTitleActivity.BitmapThread r = new CustomTitleActivity.BitmapThread(
					authorLogo, user_logo, false);
			handler.post(r);
			Handler handler2 = new Handler();
			CustomTitleActivity.BitmapThread r2 = new CustomTitleActivity.BitmapThread(
					imgs, topic_detail_img, false);
			handler2.post(r2);
		} catch (JSONException e) {
			e.printStackTrace();
		}

	}
}
